﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using TS.Commons;
using TS.Entities;

namespace TS.DataAccess
{
    /// <summary>
    /// Author: VuongPT
    /// </summary>
    public class PermissionGroupDataAccess
    {
        /// <summary>
        /// Parse to Entity
        /// </summary>
        /// <param name="dataRecord"></param>
        /// <returns></returns>
        public static PermissionGroup ParseToEntity(IDataRecord dataRecord)
        {
            return new PermissionGroup
            {
                RoleId = dataRecord["RoleId"].ToStringOrDefault().ToNumeric(),
                GroupId = dataRecord["GroupId"].ToStringOrDefault().ToNumeric(),
                IsRole = dataRecord["IsRole"].ToStringOrDefault().ToStringOrDefault(),
                GroupName = dataRecord["GroupName"].ToStringOrDefault().ToStringOrDefault(),
                RoleName = dataRecord["RoleName"].ToStringOrDefault().ToStringOrDefault(),
                RolePage = dataRecord["RolePage"].ToStringOrDefault().ToStringOrDefault(),
                Note = dataRecord["Note"].ToStringOrDefault().ToStringOrDefault()
            };
        }


        /// <summary>
        /// Insert PermissionGroup
        /// </summary>
        /// <param name="permissionGroup"></param>
        /// <returns></returns>
        public int InsertPermissionGroup(PermissionGroup permissionGroup)
        {
            int result = 0;
            if (permissionGroup != null)
            {
                var parameters = new Collection<SqlParameter>
                                                          {
                                                              new SqlParameter("@NumberPlate",permissionGroup.RoleId),
                                                              new SqlParameter("@Name",permissionGroup.GroupId)
                                                          };

                using (IDataReader reader = SqlHelper.ExecuteReader(UIPermissionGroup.InsertPermissionGroupQuery, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["PermissionId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            }

            return result;
        }

        public int UpdatePermissionGroup(int groupId, int roleId, int check)
        {
            int result = 0;
            if (groupId > 0 && roleId > 0)
            {
                var parameters = new Collection<SqlParameter>
                                                          {
                                                              new SqlParameter("@RoleId",roleId),
                                                              new SqlParameter("@GroupId",groupId),
                                                              new SqlParameter("@Check",check)
                                                          };

                using (IDataReader reader = SqlHelper.ExecuteReader(UIPermissionGroup.PermissionGroupUpdate, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["PermissionId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// Read NumberBike Pagging
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strFilter"></param>
        /// <param name="strOrderBy"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public Collection<PermissionGroup> ReadPermissionGroupPaged(int pageSize, int pageIndex, string strFilter, string strOrderBy, int GroupId, out int totalRecord)
        {
            var parameters = new Collection<SqlParameter>
                                                      {
                                                          new SqlParameter("@PageSize",pageSize),
                                                          new SqlParameter("@PageIndex",pageIndex),
                                                          new SqlParameter("@StringFilter",strFilter),
                                                          new SqlParameter("@OrderBy",strOrderBy),
                                                          new SqlParameter("@GroupId",GroupId)
                                                      };

            totalRecord = 0;

            //get result
            using (IDataReader reader = SqlHelper.ExecuteReader(UIPermissionGroup.PermissionGroupSelectPaged, parameters))
            {
                var result = new Collection<PermissionGroup>();
                while (reader.Read())
                {
                    PermissionGroup permissionGroup = ParseToEntity(reader);
                    result.Add(permissionGroup);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        totalRecord = reader["TotalRowCount"].ToNumeric();
                    }
                }
                return result;
            }
        }


        public PermissionGroup GetGroupPermissionByUserAndPage(string account, string pageName)
        {
            var collection = new Collection<SqlParameter>
                                                      {
                                                          new SqlParameter
                                                              (
                                                              "@Account",
                                                              account
                                                              ),
                                                          new SqlParameter
                                                              (
                                                              "@PageName",
                                                              pageName
                                                              )
                                                      };

            using (IDataReader iDataReader = SqlHelper.ExecuteReader(UIPermissionGroup.PermissionGroupSelectByUserAndPaged, collection))
            {
                if (iDataReader.Read())
                {
                    return ParseToEntity(iDataReader);
                }
            }
            return null;
        }
    }
}
